<?php
declare (strict_types=1);

namespace app\course\service\cert\admin;

use app\admin\logic\system\Config as SystemConfig;
use app\common\logic\Attach;
use app\common\service\BaseService;
use app\course\logic\base\admin\CourseCertConfig;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\page\PageForm;
use think\annotation\Inject;
use think\Request;

/**
 * 证书配置
 */
class Config extends BaseService
{
    /**
     * @Inject()
     *
     * @var CourseCertConfig
     */
    protected $courseCertConfigLogic;

    /**
     * 证书配置页面
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-06
     *
     * @return   [type]                         [description]
     */
    public function index(Request $request)
    {
        $place   = [
            ['title' => '不显示', 'value' => 'none'],
            ['title' => '右上角', 'value' => 'right-top'],
            ['title' => '右下角', 'value' => 'right-bottom'],
        ];
        $keyList = [
            'title'               => FormItem::make()->title('证书标题')->maxlength(30)->help('支持使用以下变量来替换成相应内容<br />1. . 课程名称: {{course_title}}<br />')->required(),
            'content'             => FormItem::make('custom')->title('证书内容')->help('支持使用以下变量来替换成相应内容<br />1. 用户昵称: {{nick_name}}<br />2. 课程名称: {{course_title}}<br />3. 学习完成年月日: {{date}}<br />4. 课程学完学时数: {{finished_class_hour}}')->required(),
            'show_sections'       => FormItem::make('SlideSwitch')->activeValue(1)->inactiveValue(0)->title('证书中显示详细课时')->help('开启后,将在证书中显示课程的课时名称'),
            'background'          => FormItem::make('custom')->title('背景图')->help('建议尺寸 1123px*794px，若图片的宽高不够，会进行平铺展示'),
            'official_seal'       => FormItem::make('custom')->title('公章')->help('建议尺寸 165px*165px，公章图片背景需透明'),
            'official_seal_place' => FormItem::make('radio')->title('公章位置')->options($place),
        ];

        $form = new PageForm;
        // 表单字段
        $form->pageKey = $keyList;
        $form->withSystemConfig();
        $key             = 'course.cert.config';
        $config          = SystemConfig::get($key, ['official_seal' => 0, 'background' => 0], $request->mhm_id);
        $config['__key'] = $key;

        // 公章地址
        if ($config['official_seal'] ?? false) {
            $config['official_seal_url'] = Attach::where('id', $config['official_seal'])->findOrEmpty()->getAttr('fileurl');
        }

        // 背景地址
        if ($config['background'] ?? false) {
            $config['background_url'] = Attach::where('id', $config['background'])->findOrEmpty()->getAttr('fileurl');
        }

        $form->datas = $config;
        return $form->send();
    }

    /**
     * 配置课程证书获取方式
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/29
     *
     * @param int $courseId
     * @return \think\Response
     */
    public function courseCertConfig(int $courseId, Request $request)
    {
        if ($request->isPost()) {
            // 保存
            $data = $request->post();
            $this->courseCertConfigLogic->saveCertConfig($courseId, $data);

            return $this->parseToData([], 1, '保存成功');
        }
        $result = $this->courseCertConfigLogic->getCertConfigByCourseId($courseId);

        return $this->parseToData($result, 1, '获取成功');
    }

}
